package com.subtlman.logframework.resp;

import com.subtlman.logframework.entity.Users;
import org.springframework.http.HttpStatus;

import java.util.List;

/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2022/11/11/23:43
 * @Description: return object entity class
 */
public final class ServiceResp<T> implements java.io.Serializable {

    /**
     * HttpStatus
     */
    private Integer status;
    /**
     * message
     */
    private String msg;
    /**
     * Detailed Inform
     */
    private InformResp inform;
    /**
     * Response data
     */
    private T data;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public InformResp getInform() {
        return inform;
    }

    public void setInform(InformResp inform) {
        this.inform = inform;
    }

    @Override
    public String toString() {
        return "ServiceResp{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", inform='" + inform + '\'' +
                ", data=" + data +
                '}';
    }

    public ServiceResp() {
    }

    public static ServiceResp<?> getInstance() {
        return new ServiceResp();
    }

    public ServiceResp<?> error() {
        this.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        this.setMsg("error");
        this.setInform(InformResp.getInstance());
        this.setData(null);
        return this;
    }

    public ServiceResp<?> error(String msg,Exception e) {
        this.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        this.setMsg(msg);
        this.setInform(InformResp.getInstance().error(e));
        this.setData(null);
        return this;
    }

    public ServiceResp<?> error(int status, String msg,Exception e) {
        this.setStatus(status);
        this.setMsg(msg);
        this.setInform(InformResp.getInstance().error(e));
        this.setData(null);
        return this;
    }

    public ServiceResp<?> error(InformResp inform) {
        this.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        this.setMsg("error");
        this.setInform(inform);
        this.setData(null);
        return this;
    }

    public ServiceResp<?> error(String msg, InformResp inform) {
        this.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        this.setMsg(msg);
        this.setInform(inform);
        this.setData(null);
        return this;
    }

    public ServiceResp<?> success() {
        this.setStatus(HttpStatus.OK.value());
        this.setMsg("success");
        this.setInform(InformResp.getInstance().success());
        this.setData(null);
        return this;
    }

    public ServiceResp<?> success(String msg) {
        this.setStatus(HttpStatus.OK.value());
        this.setMsg(msg);
        this.setInform(InformResp.getInstance().success());
        this.setData(null);
        return this;
    }

    public ServiceResp<?> success(T data, String msg) {
        this.setStatus(HttpStatus.OK.value());
        this.setMsg(msg);
        this.setInform(InformResp.getInstance().success());
        this.setData(data);
        return this;
    }

    public ServiceResp<?> success(List<Users> data) {
        this.setStatus(HttpStatus.OK.value());
        this.setMsg("success");
        this.setInform(InformResp.getInstance().success());
        this.setData((T) data);
        return this;
    }

    public boolean isSuccess() {
        return this.getStatus() == HttpStatus.OK.value();
    }

}
